package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.BsnGonggaoEntity;
import io.renren.modules.generator.service.BsnGonggaoService;
import io.renren.common.utils.ConstantLocal;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 项目进展处理
 */
@RestController
@RequestMapping("generator/bsnXmjz")
public class BsnXmjzController {
    @Autowired
    private BsnGonggaoService bsnGonggaoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:bsngonggao:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bsnGonggaoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("generator:bsngonggao:info")
    public R info(@PathVariable("id") String id){
			BsnGonggaoEntity bsnGonggao = bsnGonggaoService.selectById(id);
			if (bsnGonggao == null) {
				bsnGonggao = new BsnGonggaoEntity();
			}
        return R.ok().put("bsnGonggao", bsnGonggao);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:bsngonggao:save")
    public R save(@RequestBody BsnGonggaoEntity bsnGonggao){
			bsnGonggaoService.insert(bsnGonggao);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:bsngonggao:update")
    public R update(@RequestBody BsnGonggaoEntity bsnGonggao){
			bsnGonggaoService.updateById(bsnGonggao);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:bsngonggao:delete")
    public R delete(@RequestBody String[] ids){
			bsnGonggaoService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 保存项目进展
     */
    @RequestMapping("/saveXmjz")
    public R saveXmjz(@RequestBody BsnGonggaoEntity bsnGonggao){
    	
    	if (StringUtils.isBlank(bsnGonggao.getType())){
    		bsnGonggao.setId(ConstantLocal.GONGGAO_TYPE_XMJZ);
    		bsnGonggao.setType(ConstantLocal.GONGGAO_TYPE_XMJZ);
    		bsnGonggao.setBiaoti(ConstantLocal.GONGGAO_TYPE_XMJZ);
    		bsnGonggao.setCreateTime(new Date());
    		bsnGonggaoService.insert(bsnGonggao);
    	} else {
    		bsnGonggao.setCreateTime(new Date());
    		bsnGonggaoService.updateById(bsnGonggao);
    	}
        return R.ok();
    }
}
